package com.tsky.yyj.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tsky.casing.QueryBean;
import com.tsky.casing.action.BackAction;
import com.tsky.common.ActionUtil;
import com.tsky.common.BeanUtil;
import com.tsky.system.entity.Constant;
import com.tsky.yyj.entity.Area;
import com.tsky.yyj.json.YYJJsonFormat;
import com.tsky.yyj.manager.BaseManager;

@Component("areaAction")
@Scope("prototype")
/*@Action(value = "area")  
@Results({ @Result(name = "query", location = "/application/yyj/area/edit.jsp") })*/
public class AreaAction extends BackAction {
	@Autowired
	private BaseManager entityManager;

	/* bean properties */
	private String id;
	private String ids;
	private String status;
	private String name;
	private String city;// @城市

	public void grid() {
		QueryBean grid = new QueryBean();
		BeanUtil.copyProperties(grid, this);

		entityManager.query(grid, Area.class);

		String jsonData = YYJJsonFormat.areaToJson(grid);
		ActionUtil.ajaxWrite(jsonData);
	}

	public void add() {
		Area entity = new Area();
		BeanUtil.copyProperties(entity, this);
		entity.setId(BeanUtil.getID());

		Constant cityObj = new Constant();
		if(!BeanUtil.isEmpty(city)){
			cityObj = entityManager.getEntity(Constant.class, city);
			entity.setCity(cityObj);
		}
		
		boolean result = entityManager.saveEntity(entity);
		ActionUtil.ajaxWrite(result);
	}

	public void delete() {
		String[] idset = ids.split(",");
		boolean result = entityManager.delEntity(Area.class, idset);
		ActionUtil.ajaxWrite(result);
	}

	public String query() {
		Area entity = entityManager.getEntity(Area.class, id);
		ActionUtil.setToEL("areaEnt", entity);
		return "query";
	}

	public void update() {
		Area entity = entityManager.getEntity(Area.class, id);
		BeanUtil.copyProperties(entity, this);

		Constant cityObj = new Constant();
		if(!BeanUtil.isEmpty(city)){
			cityObj = entityManager.getEntity(Constant.class, city);
			entity.setCity(cityObj);
		}
		
		boolean result = entityManager.updateEntity(entity);
		ActionUtil.ajaxWrite(result);
	}

	public BaseManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(BaseManager entityManager) {
		this.entityManager = entityManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

}
